﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.ZJWWorkOrderTwo;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.ZJWWorkOrderTwoCommands;
using Yitter.IdGenerator;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.ZJWWorkOrderTwoCommandHandlers
{
    /// <summary>
    /// 新增医疗卡
    /// </summary>
    public class AddMedicalCardCommandHandler : IRequestHandler<AddMedicalCardCommand, APIResult<int>>
    {
        private readonly SqlSugarContext sqlSugar;
        private readonly IMapper mapper;

        public AddMedicalCardCommandHandler(SqlSugarContext sqlSugar, IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回受影响的行数</returns>
        public Task<APIResult<int>> Handle(AddMedicalCardCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var entyit = mapper.Map<MedicalCard>(result);
            entyit.CardNumber = YitIdHelper.NextId().ToString();
            entyit.CardStatus = "正常";
            var r = sqlSugar.db.Insertable(entyit).ExecuteCommand();
            if (r > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            return Task.FromResult(result);
        }

    }
}
